'use strict';
const uniID = require('uni-id-common')
const ASE = require('ase')
exports.main = async (event, context) => {
	const uniIDIns = uniID.createInstance({
		context: context,
	})
	const res = await uniIDIns.checkToken(event.uniIdToken)
	const db = uniCloud.databaseForJQL({
		event,
		context
	});
	db.setUser({
		role: ['admin']
	})
	event = JSON.parse(ASE.AES.decrypt(event.data, 'yclycl1314520520', '9740022097400220'))
	let userinfo = await db.collection("uni-id-users").where({
		_id: res.uid
	}).get({
		getOne: true
	});
	var vipday;
	if (userinfo.data.vip == 0 || new Date().getTime() > userinfo.data.vip) {
		vipday = new Date().getTime() + event.value * 86400000;
	} else {
		vipday = userinfo.data.vip + event.value * 86400000;
	}
	await db.collection("uni-id-users").where({
		_id: res.uid
	}).update({
		vip: vipday
	})
	return vipday
};